package indi.zjh.dm.startup;

import java.io.File;
import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;

import org.dom4j.DocumentException;

import dm.bsi.Deployment;
import dm.bsi.Log;
import dm.bsi.MSDeployment;
import dm.bsi.MSSDeployment;
import dm.bsi.Node;
import dm.bsi.WDeployment;
import dm.daemon.DeploymentDaemon;
import dm.daemon.NodeDaemon;
import indi.zjh.dp.access.HttpServer;
import indi.zjh.dp.db.Configuration;
import indi.zjh.dp.db.DataSource;
import indi.zjh.dp.db.Session;
import indi.zjh.dp.error.MySQLConnectionUseupException;
import indi.zjh.dp.orm.Query;
import indi.zjh.dp.orm.Result;
import indi.zjh.dp.util.RedisUtil;
import indi.zjh.dp.vc.Commit;
import indi.zjh.dp.vc.Version;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try
		{
			Configuration.getInstance().setVersion(new Version());
			Configuration.getInstance().getVersion().analyzeClassRelation();
			File file = new File("first");
			if(!file.exists())
			{
				file.createNewFile();
				Configuration.getInstance().getVersion().clear();
				Configuration.getInstance().getVersion().initial();
			}
			
			// 初始化缓存
			RedisUtil.init();
		
			// 初始化
			init();
			
			Commit commit = new Commit();
			commit.submit("日期:"+new Date().getTime());
			
			
//			new Thread(new TaskDispatcher()).start();
			HttpServer server = new HttpServer();
			server.start(8080);
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private static void init() throws SQLException, MySQLConnectionUseupException, DocumentException, Exception
	{
		// 忽略写入数据源的类
		DataSource.ignore(Log.class);
		DataSource.ignore(Node.class);
		
		// 提交版本
		Commit commit = new Commit();
		commit.submit(new Date().getTime()+"");
		
		
		Session session = Configuration.getSessionFactory().createSession();
		try
		{
			// 查找已经存在的node并且启动
			Query<Node> query = new Query<Node>(session);
			Result<Node> result = query.find(Node.class).result();
			Iterator<Node> iter = result.iterator();
			while(iter.hasNext())
			{
				Node node = iter.next();
				node.setReady(false);
				session.save(node).commit();
				NodeDaemon daemon = new NodeDaemon(node);
				new Thread(daemon).start();
			}
			
			// 查找已经存在的Deployment并且启动
			Query<WDeployment> q1 = new Query<WDeployment>(session);
			Result<WDeployment> r1 = q1.find(WDeployment.class).result();
			Iterator<WDeployment> itr1 = r1.iterator();
			while(itr1.hasNext())
			{
				WDeployment deployment = itr1.next();
				DeploymentDaemon daemon = new DeploymentDaemon(deployment);
				new Thread(daemon).start();
			}
			
			Query<MSDeployment> q2 = new Query<MSDeployment>(session);
			Result<MSDeployment> r2 = q2.find(MSDeployment.class).result();
			Iterator<MSDeployment> itr2 = r2.iterator();
			while(itr2.hasNext())
			{
				MSDeployment deployment = itr2.next();
				DeploymentDaemon daemon = new DeploymentDaemon(deployment);
				new Thread(daemon).start();
			}
			
			Query<MSSDeployment> q3 = new Query<MSSDeployment>(session);
			Result<MSSDeployment> r3 = q3.find(MSSDeployment.class).result();
			Iterator<MSSDeployment> itr3 = r3.iterator();
			while(itr3.hasNext())
			{
				MSSDeployment deployment = itr3.next();
				DeploymentDaemon daemon = new DeploymentDaemon(deployment);
				new Thread(daemon).start();
			}
		}
		catch(Exception e)
		{
			session.rollback();
			e.printStackTrace();
		}
		finally {
			try {
				session.close();
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
